package demo4.controller;

import com.tedu.InputUtil;
import demo4.mapper.IUserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLOutput;
import java.util.List;
import java.util.Scanner;

/*
* Controller 控制器,是MVC中的C(后面学习MVC是会详细介绍)
* */
@Component
public class UserController {

    @Autowired
    private IUserMapper userMapper;
    public  void start() {
        System.out.println("请选择对应的操作,输入序号:");
        System.out.println("请输入1用户注册2登录3修改用户信息,4:用户列表5删除用户6再说");
        Scanner scanner = new Scanner(System.in);
        int num = Integer.parseInt(scanner.nextLine());

        switch(num){
            case 1:
                regUser();
                break;
            case 2:
                loginUser();
                break;
            case 3:
                updateUser();
                break;
            case 4:
                listUser();
                break;
            case 5:
                deleteUser();
                break;
            default:
                System.out.println("还在开发中");
                break;

        }

    }

    public void updateUser(){
        System.out.println("请输入你要修改的信息");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请先输入您的用户名");
        String username = scanner.nextLine();
        System.out.println("请输入你要设置的密码");
        String password = scanner.nextLine();
        System.out.println("请输入你要设置的昵称");
        String nickname = scanner.nextLine();
        System.out.println("请输入你要设置的年龄");
        int age = Integer.parseInt(scanner.nextLine());


        User user = new User();
        user.setUsername(username);
        user.setNickname(nickname);
        user.setPassword(password);
        user.setAge(age);
        int update = userMapper.update(user);
        System.out.println(update>0?"修改成功" : "用户名无效");

    }




    public void regUser(){
        System.out.println("开始处理用户注册");
        //User user = new User(null,username,password,nickname,age);
        //判断是否为重复用户
        User user = InputUtil.getInputObject(new User());

        if(userMapper.selectByUsername(user.getUsername())!=null){
            System.out.println("该用户已存在");
        }else{

            int num = userMapper.insert(user);
            System.out.println(num>0?"注册成功":"注册失败");

        }

    }

    public void listUser(){
        System.out.println("用户列表");
        List<User> userList = userMapper.selectAll();
        for(User user: userList){
            System.out.println(user);
        }

    }

    public void deleteUser(){
        Scanner scanner= new Scanner(System.in);
        System.out.println("请输入您要删除的用户名");
        String username = scanner.nextLine();

        int num = userMapper.deleteByUsername(username);
        System.out.println(num>0?"删除成功":"删除失败");
    }



    public void loginUser(){
        System.out.println("欢迎登录");
        /*
        *
        *
        * */
        User user = InputUtil.getInputObject(new User());

        //boolean b = userMapper.selectUser(user.getUsername(), user.getPassword());
//        if(b){
//            System.out.println("登录成功");
//        }else{
//            System.out.println("登录失败");
//        }
        //根据用户名提取该用户的注册信息
        User user1 = userMapper.selectByUsername(user.getUsername());
        if(user1==null){//用户不存在
            System.out.println("用户名输入有误");
        }else{
            //比较密码
            if(user1.getPassword().equals(user.getPassword())){//比较成功
                System.out.println("登录成功，欢迎【"+user1.getNickname()+"】回来");
            }else{
                System.out.println("密码输入有误!");
            }
        }
    }
}
